package dao;

import java.util.List;

import hibernate.HibernateUtil;
import negocio.Vehiculo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dto.VehiculoDTO;
import entities.CargaEntity;
import entities.ClienteEntity;
import entities.VehiculoEntity;

public class VehiculoDAO {
	private static VehiculoDAO instancia = null;
	private static SessionFactory sf = null;
	
	private VehiculoDAO(){}
	
	public static VehiculoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new VehiculoDAO();
		} 
		return instancia;
	}
	
	public void grabarVehiculo(VehiculoEntity vehiculo){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(vehiculo);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void actualizarVehiculo(VehiculoEntity vehiculo){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(vehiculo);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public VehiculoEntity LoadVehiculo(int id) 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		VehiculoEntity ve = (VehiculoEntity) session.get(VehiculoEntity.class, id);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return ve;
	}
	
	@SuppressWarnings("unchecked")
	public List<VehiculoEntity> vehiculosMantenimiento() 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		List<VehiculoEntity> vehiculosManteniemientoEntity = null;
		vehiculosManteniemientoEntity = session.createQuery("select v from VehiculoEntity v inner join v.operaciones o").list();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return vehiculosManteniemientoEntity;
				
	}
	
	public List<VehiculoEntity> vehiculosSinMantenimiento() 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		List<VehiculoEntity> vehiculosManteniemientoEntity = null;
		vehiculosManteniemientoEntity = session.createQuery("select v from VehiculoEntity v where not exists (select 1 from VehiculoEntity v1 inner join v1.operaciones o where v.idVehiculo=v1.idVehiculo)").list();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return vehiculosManteniemientoEntity;
				
	}

	public VehiculoEntity obtenerVehiculoVacio(double volumen) {
		Session session = sf.openSession();
		session.beginTransaction();
		List<VehiculoEntity> vehiculos = session.createQuery("select v from VehiculoEntity v where (v.volumen*0.7) > :volumen ").setParameter("volumen", volumen).list();
		for (VehiculoEntity ve : vehiculos)
		{
			if(EnvioDAO.getInstancia().obtenerVehiculoDisponible(ve))
			{
				session.flush();
				session.getTransaction().commit();
				session.close();
				return ve;
			}
			
		}
		session.flush();
		session.getTransaction().commit();
		session.close();
		return null;
	}
}